package com.ueip.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ueip.backend.mapper.CityMapper;
import com.ueip.backend.pojo.City;
import com.ueip.backend.pojo.User;
import com.ueip.backend.service.CityService;
import com.ueip.backend.utils.IdentityAuthenticationUtil;
import com.ueip.backend.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Service
public class CityServiceImpl implements CityService {
    @Resource
    private CityMapper cityMapper;

    @Override
    public Result add(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        System.out.println(data);
        try {
            Integer pid = Integer.parseInt(data.get("pid"));
            String name = data.get("name");
            if (name == null || name.length() == 0 || name.length() > 20) {
                return Result.error("城市名称过短或过长");
            }
            City prov = cityMapper.selectById(pid);
            if (prov == null) {
                return Result.error("该省份不存在或被删除");
            }
            QueryWrapper<City> cityQueryWrapper = new QueryWrapper<>();
            cityQueryWrapper.eq("pid", pid);
            List<City> cities = cityMapper.selectList(cityQueryWrapper);
            Integer maxId = 0;
            Integer id = null;
            if (cities.isEmpty()) {
                id = prov.getId() + 1;
            } else {
                for (City city : cities) {
                    if (city.getId() > maxId) {
                        maxId = city.getId();
                    }
                }
                id = maxId + 1;
            }
            cityMapper.insert(new City(id, name, pid));
            return  Result.ok("add success");
        } catch (Exception e) {
            return Result.error("数据有误");
        }
    }

    @Override
    public Result update(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            Integer id = Integer.parseInt(data.get("id"));
            Integer pid = Integer.parseInt(data.get("pid"));
            String name = data.get("name");

            City oldCityInfo = cityMapper.selectById(id);
            if (oldCityInfo == null) {
                return Result.error("该城市不存在或已被删除");
            }

            if (name == null || name.length() == 0 || name.length() > 20) {
                return Result.error("城市名称过短或过长");
            }
            City prov = cityMapper.selectById(pid);
            if (prov == null) {
                return Result.error("该省份不存在或被删除");
            }

            City newCityInfo = new City();
            newCityInfo.setPid(pid);
            newCityInfo.setName(name);

            Integer newId = 0;

            // 这里需要判断下，更改后城市的pid与原来的pid是否一致
            // 如果不一致，需要先把原来的city删除掉，在将新的city插入到数据库中
            // 否则只需要执行update就行了

            if (oldCityInfo.getPid() != pid) {
                QueryWrapper<City> cityQueryWrapper = new QueryWrapper<>();
                cityQueryWrapper.eq("pid", pid);
                List<City> cities = cityMapper.selectList(cityQueryWrapper);
                for (City city : cities) {
                    if (city.getId() > newId) {
                        newId = city.getId();
                    }
                }
                newId ++ ;
                newCityInfo.setId(newId);
                cityMapper.deleteById(oldCityInfo);
                cityMapper.insert(newCityInfo);
            } else {
                newId = oldCityInfo.getId();
                newCityInfo.setId(newId);
                cityMapper.updateById(newCityInfo);
            }
            return Result.ok("update success");
        } catch (Exception e) {
            return Result.error("数据有误");
        }
    }

    @Override
    public Result getAll() {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        return Result.ok(cityMapper.selectList(null));
    }

    @Override
    public Result getProvinces() {
        User curUser = IdentityAuthenticationUtil.getCurUser();

        Integer pid = -1;
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        return Result.ok(cityMapper.selectList(queryWrapper));
    }

    @Override
    public Result getCitiesByPid(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            Integer pid = Integer.parseInt(data.get("pid"));
            QueryWrapper<City> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid", pid);
            return Result.ok(cityMapper.selectList(queryWrapper));
        } catch (Exception e) {
            return Result.error("pid 有误或不存在");
        }

    }

    @Override
    public Result getNameById(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            BigInteger id = new BigInteger(data.get("id"));
            City city = cityMapper.selectById(id);
            if (city == null) {
                return Result.error("改学校已被删除或不存在");
            }
            return Result.ok(city.getName());
        } catch (Exception e) {
            return Result.error("数据有误");
        }
    }

    @Override
    public Result deleteById(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            Integer id = Integer.parseInt(data.get(("id")));
            City city = cityMapper.selectById(id);
            if (city == null) {
                return Result.error("该城市不存在或已被删除");
            }
            cityMapper.deleteById(id);
            return Result.ok("delete success");
        } catch (Exception e) {
            return  Result.error("数据有误");
        }
    }
}
